//////////////////////////////////////////////////////////////////////
//
//	Copyright 2010-2011 Rogo's Team. All Rights Reserved
//
//////////////////////////////////////////////////////////////////////
package com.rogo.weibo.platform.netease.api.directmessage
{
	import com.rogo.weibo.core.DoAPI;
	import com.rogo.weibo.platform.netease.api.oauth.Oauth;
	import com.rogo.weibo.utils.HttpParameter;
	import com.rogo.weibo.utils.OauthUrlUtil;
	
	import flash.net.URLRequestMethod;
	
	import json.JSON;
	
	/**
	 * 网易微博API 
	 * 1.1.7 私信
	 * 
	 * @see http://rogo.sinaapp.com
	 * @see http://open.t.163.com
	 * @author rogo
	 */	
	public class DoDirectMessage extends DoAPI
	{
		public function DoDirectMessage(dataHandler:Function, errorHandler:Function)
		{
			super(dataHandler, errorHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//	Constants
		//
		//--------------------------------------------------------------------------
		
		private static const URL_DIRECTMESSAGE:String = 'http://api.t.163.com/direct_messages.json';
		public static const CMD_DIRECTMESSAGE:String = 'cmdDirectMessage';
		
		private static const URL_DIRECTMESSAGE_SEND:String = 'http://api.t.163.com/direct_messages/sent.json';
		public static const CMD_DIRECTMESSAGE_SEND:String = 'cmdDirectMessageSend';
		
		private static const URL_NEW_DIRECTMESSAGE:String = 'http://api.t.163.com/direct_messages/new.json';
		public static const CMD_NEW_DIRECTMESSAGE:String = 'cmdNewDirectMessage';
		
		private static const URL_DESTROY_DIRECTMESSAGE:String = 'http://api.t.163.com/direct_messages/destroy/id.json';
		public static const CMD_DESTROY_DIRECTMESSAGE:String = 'cmdDestroyDirectMessage';
		
		private static const URL_GROUP_DIRECTMESSAGE:String = 'http://api.t.163.com//direct_messages/grouped.json';
		public static const CMD_GROUP_DIRECTMESSAGE:String = 'cmdGroupDirectMessage';
		
		private static const URL_SESSION_DIRECTMESSAGE:String = 'http://api.t.163.com/direct_messages/session.json';
		public static const CMD_SESSION_DIRECTMESSAGE:String = 'cmdSessionDirectMessage';
		
		private static const URL_DESTROY_SESSION_DIRECTMESSAGE:String = 'http://api.t.163.com/direct_messages/session/delete/id.json';
		public static const CMD_DESTROY_SESSION_DIRECTMESSAGE:String = 'cmdDestroySessionDirectMessage';
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 获取当前用户私信列表
		 * 
		 * @param since_id 可选参数，上一页最后一条私信的id
		 * @param count 可选参数，获取私信的数量
		 */		
		public function getDirectMessage(since_id:String, count:String):void
		{
			var paras:Array = [];
			if (since_id != null && since_id != '')
				paras.push(new HttpParameter('since_id', since_id));
			if (count != null && count != '')
				paras.push(new HttpParameter('count', count));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			
			var url:String = OauthUrlUtil.getOauthUrl(URL_DIRECTMESSAGE, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_DIRECTMESSAGE, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_DIRECTMESSAGE, params);
			}
		}
		
		/**
		 * 获取当前用户发送的私信列表
		 * 
		 * @param since_id 可选参数，上一页最后一条私信的id
		 * @param count 可选参数，获取私信的数量
		 */	
		public function getDirectMessageSend(since_id:String, count:String):void
		{
			var paras:Array = [];
			if (since_id != null && since_id != '')
				paras.push(new HttpParameter('since_id', since_id));
			if (count != null && count != '')
				paras.push(new HttpParameter('count', count));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			
			var url:String = OauthUrlUtil.getOauthUrl(URL_DIRECTMESSAGE_SEND, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_DIRECTMESSAGE_SEND, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_DIRECTMESSAGE_SEND, params);
			}
		}
		
		/**
		 * 发送一条私信
		 * 
		 * @param user 必选参数，收信用户的昵称，即name
		 * @param text 必选参数，私信内容
		 */		
		public function newDirectMessage(user:String, text:String):void
		{
			var paras:Array = [];
			if (user != null)
				paras.push(new HttpParameter('user', user));
			if (text != null)
				paras.push(new HttpParameter('text', text));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.POST;
			
			paras = OauthUrlUtil.getOauthPostParas(URL_NEW_DIRECTMESSAGE, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			var postParam:String = OauthUrlUtil.normalizeRequestParameters(paras);
			
			send(URL_NEW_DIRECTMESSAGE, 
				resultHandler, 
				errorHandler, 
				URLRequestMethod.POST,
				postParam);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_NEW_DIRECTMESSAGE, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_NEW_DIRECTMESSAGE, params);
			}
		}
		
		/**
		 * 删除一条私信
		 * 
		 * @param id 必选参数，删除的私信id
		 */		
		public function destroyDirectMessage(id:String):void
		{
			var paras:Array = [];
			if (id != null)
				paras.push(new HttpParameter('id', id));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.POST;
			
			paras = OauthUrlUtil.getOauthPostParas(URL_DESTROY_DIRECTMESSAGE, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			var postParam:String = OauthUrlUtil.normalizeRequestParameters(paras);
			
			send(URL_DESTROY_DIRECTMESSAGE, 
				resultHandler, 
				errorHandler, 
				URLRequestMethod.POST,
				postParam);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_DESTROY_DIRECTMESSAGE, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_DESTROY_DIRECTMESSAGE, params);
			}
		}
		
		/**
		 * 会话分组方式获取当前用户私信
		 * 
		 * @param since_id 可选参数，上一页最后一条会话的id
		 * @param count 可选参数，获取私信的数量
		 */		
		public function getGroupDirectMessage(since_id:String, count:String):void
		{
			var paras:Array = [];
			if (since_id != null && since_id != '')
				paras.push(new HttpParameter('since_id', since_id));
			if (count != null && count != '')
				paras.push(new HttpParameter('count', count));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			
			var url:String = OauthUrlUtil.getOauthUrl(URL_GROUP_DIRECTMESSAGE, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_GROUP_DIRECTMESSAGE, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_GROUP_DIRECTMESSAGE, params);
			}
		}
		
		/**
		 * 査看会话分组下的所有私信
		 * 
		 * @param user_id （可选） 注：会话列表中的会话id就是 user_id
		 * @param screen_name 用户的screen_name，个性网址（可选）
		 * @param name 昵称（可选）
		 * @param since_id 上一页的最后一条私信id
		 * @param count 可选参数，获取私信的数量
		 */		
		public function getSessionDirectMessage(user_id:String, screen_name:String, name:String, since_id:String, count:String):void
		{
			var paras:Array = [];
			if (user_id != null && user_id != '')
				paras.push(new HttpParameter('user_id', user_id));
			if (screen_name != null && screen_name != '')
				paras.push(new HttpParameter('screen_name', screen_name));
			if (name != null && name != '')
				paras.push(new HttpParameter('name', name));
			if (since_id != null && since_id != '')
				paras.push(new HttpParameter('since_id', since_id));
			if (count != null && count != '')
				paras.push(new HttpParameter('count', count));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			
			var url:String = OauthUrlUtil.getOauthUrl(URL_SESSION_DIRECTMESSAGE, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_SESSION_DIRECTMESSAGE, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_SESSION_DIRECTMESSAGE, params);
			}
		}
		
		/**
		 * 删除一组私信会话
		 * 
		 * @param id 必选参数，删除的私信会话id (相关用户的user id)
		 */		
		public function destroySessionDirectMessage(id:String):void
		{
			var paras:Array = [];
			if (id != null)
				paras.push(new HttpParameter('id', id));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.POST;
			
			paras = OauthUrlUtil.getOauthPostParas(URL_DESTROY_SESSION_DIRECTMESSAGE, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			var postParam:String = OauthUrlUtil.normalizeRequestParameters(paras);
			
			send(URL_DESTROY_SESSION_DIRECTMESSAGE, 
				resultHandler, 
				errorHandler, 
				URLRequestMethod.POST,
				postParam);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_DESTROY_SESSION_DIRECTMESSAGE, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_DESTROY_SESSION_DIRECTMESSAGE, params);
			}
		}
		
		private function resultResponse(cmd:String, params:String):void
		{
			var data:Object = JSON.decode(params);
			_dataHandler(cmd, data);
		}
		
		private function errorResponse(cmd:String, params:String):void
		{
			var data:Object = JSON.decode(params);
			_errorHandler(cmd, data);
		}
	}
}